package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类信息的功能接口
     * @param categoryDTO
     * @return
     */
    @PostMapping
    public Result save(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类：{}",categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success();
    }

    /**
     * 根据id删除分类信息
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(Long id){
        log.info("根据id删除分类:{}",id);
        categoryService.delete(id);
        return Result.success();
    }

    /**
     * 分类查询分类信息
     * @param categoryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryDTO){

        log.info("分页查询分类信息:{}",categoryDTO);
        PageResult categoryPageResult= categoryService.page(categoryDTO);
        return Result.success(categoryPageResult);
    }

    /**
     * 根据类型查询分类信息
     * @param type
     * @return
     */
    @GetMapping("/list")
    public Result<List<Category>> list(Integer type){
        log.info("按照类型查询分类信息:{}",type);
        List<Category> list=categoryService.list(type);
        return Result.success(list);
    }

    /**
     * 根据id修改分类的状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result status(@PathVariable("status") Integer status,Long id){
        log.info("修改分类的状态:{},{}",status,id);
        categoryService.status(status,id);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody CategoryDTO category){
        log.info("修改分类信息:{}",category);
        categoryService.update(category);
        return Result.success();
    }

}
